﻿// WebZHX.API/Controllers/FlourBatchOutNewController.cs
using Common.Models;
using Core.Entities;
using Infrastructure.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace WebZHX.Controllers
{
    /// <summary>
    /// 面粉出库批次控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class FlourBatchOutNewController : BaseController<FlourBatchOutNew, int>
    {
        private readonly FlourBatchOutNewService _flourBatchOutNewService;

        public FlourBatchOutNewController(
            BaseService<FlourBatchOutNew, int> service,
            FlourBatchOutNewService flourBatchOutNewService)
            : base(service)
        {
            _flourBatchOutNewService = flourBatchOutNewService;
        }

        /// <summary>
        /// 根据批次号查询出库记录
        /// </summary>
        /// <param name="batchNumber">批次号</param>
        /// <returns>出库记录列表</returns>
        [HttpGet("batch/{batchNumber}")]
        public async Task<ActionResult<IEnumerable<FlourBatchOutNew>>> GetByBatchNumber(string batchNumber)
        {
            var result = await _flourBatchOutNewService.GetByBatchNumberAsync(batchNumber);
            return Ok(result);
        }

        /// <summary>
        /// 根据日期范围查询出库记录
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>出库记录列表</returns>
        [HttpGet("date-range")]
        public async Task<ActionResult<IEnumerable<FlourBatchOutNew>>> GetByDateRange(
            [FromQuery] DateTime startDate,
            [FromQuery] DateTime endDate)
        {
            var result = await _flourBatchOutNewService.GetByDateRangeAsync(startDate, endDate);
            return Ok(result);
        }

        /// <summary>
        /// 获取出库统计信息
        /// </summary>
        /// <returns>统计信息</returns>
        [HttpGet("statistics")]
        public async Task<ActionResult<dynamic>> GetStatistics()
        {
            var result = await _flourBatchOutNewService.GetOutStatisticsAsync();
            return Ok(result);
        }

        /// <summary>
        /// 创建出库记录
        /// </summary>
        /// <param name="model">出库记录数据</param>
        /// <returns>创建的记录</returns>
        [HttpPost("create")]
        public async Task<ActionResult<FlourBatchOutNew>> CreateFlourBatchOut(FlourBatchOutNew model)
        {
            // 可以在这里添加业务验证逻辑
            if (model.DrawnWeight <= 0 || model.TotalWeight <= 0)
            {
                return BadRequest("数量和重量必须大于0");
            }

            var result = await _flourBatchOutNewService.CreateAsync(model);
            return CreatedAtAction(nameof(Get), new { id = result.Id }, result);
        }

        /// <summary>
        /// 批量创建出库记录
        /// </summary>
        /// <param name="models">出库记录列表</param>
        /// <returns>创建结果</returns>
        [HttpPost("batch-create")]
        public async Task<ActionResult> BatchCreate(List<FlourBatchOutNew> models)
        {
            foreach (var model in models)
            {
                await _flourBatchOutNewService.CreateAsync(model);
            }

            return Ok(new { message = $"成功创建{models.Count}条记录" });
        }

        /// <summary>
        /// 高级分页查询
        /// </summary>
        /// <param name="filter">查询过滤器</param>
        /// <returns>分页结果</returns>
        [HttpPost("advanced-paged")]
        public async Task<ActionResult<PagedResult<FlourBatchOutNew>>> AdvancedPagedQuery(QueryFilter filter)
        {
            var result = await _flourBatchOutNewService.GetPagedAsync(filter);
            return Ok(result);
        }
    }
}